function outcome=FVRADE(func,totaltime,problem_size,pop_size)

rand('seed', sum(100 * clock));
max_nfes = 10000 * problem_size; 
foptimum = [100:100:3000];
optimum = foptimum(func);
lu = [-100 * ones(1, problem_size); 100 * ones(1, problem_size)];
fhd=str2func('cec17_func'); 
  %% Record the best results
  outcome = []; 

  fprintf('\n-------------------------------------------------------\n')
  fprintf('Function = %d, Dimension size = %d\n', func, problem_size) 
    file_str=['fitness_',num2str(problem_size),'_',num2str(pop_size),'_',num2str(func),'.txt'];
    path=['./Results/Dim',num2str(problem_size),'/',file_str];
    fileID=fopen(path,'w');


  for run_id = 1 : totaltime
      arc_rate = 1;  
    %%Calculated weight       
    Gamma=0.1;
    weight=[];
    Probablity=[];
    sum1=0;
    for i=1:pop_size
        weight(i)=1/(Gamma*pop_size*sqrt(2*pi))*exp(-((i-1)^2/(2*Gamma^2*pop_size^2)));
        sum1=sum1+ weight(i);
    end
    %%Computational selection probability
    for i=1:pop_size
        Probablity(i)=weight(i)/sum1;
    end
    for i=2:pop_size
        Probablity(i)=Probablity(i)+Probablity(i-1);
    end


    %% Initialize the main population
    popold = repmat(lu(1, :), pop_size, 1) + rand(pop_size, problem_size) .* (repmat(lu(2, :) - lu(1, :), pop_size, 1));
    pop = popold; % the old population becomes the current population

    fitness = feval(fhd, pop', func);
    fitness = fitness';
    
    Gbest=[];

    nfes = 0;
    bsf_fit_var = 1e+30;
    bsf_solution = zeros(1, problem_size);

    %%%%%%%%%%%%%%%%%%%%%%%% for out
    for i = 1 : pop_size
      nfes = nfes + 1;

    if fitness(i) < bsf_fit_var
	bsf_fit_var = fitness(i);
	bsf_solution = pop(i, :);
      end
      if nfes > max_nfes; break; end
    end
    %%%%%%%%%%%%%%%%%%%%%%%% for out


     memory_sf = 0.5 ;
     memory_cr = 0.5 ;

    archive.NP = arc_rate * pop_size; % the maximum size of the archive
    archive.pop = zeros(0, problem_size); % the solutions stored in te archive
    archive.funvalues = zeros(0, 1); % the function value of the archived solutions
    archive.np=1;%The number of stored solutions
    %% main loop
    while nfes < max_nfes

      pop = popold; % the old population becomes the current population
      [temp_fit, sorted_index] = sort(fitness, 'ascend');%sorted_index
   
      mu_sf = ones(pop_size,1)*memory_sf;
      mu_cr = ones(pop_size,1)* memory_cr;

      %% for generating crossover rate
      cr = normrnd(mu_cr, 0.1);
      term_pos = find(mu_cr == -1);
      cr(term_pos) = 0;
      cr = min(cr, 1);
      cr = max(cr, 0);
      [tem_cr,~]=sort(cr, 'ascend');
      cr(sorted_index)=tem_cr;
      %% for generating scaling factor
      sf = mu_sf + 0.1 * tan(pi * (rand(pop_size, 1) - 0.5));
      pos = find(sf <= 0);

      while ~ isempty(pos)
	sf(pos) = mu_sf(pos) + 0.1 * tan(pi * (rand(length(pos), 1) - 0.5));
	pos = find(sf <= 0);
      end

      sf = min(sf, 1); 
      
      r0 = [1 : pop_size];
      popAll = [pop; archive.pop];
      [r1, r2] = gnR1R2(pop_size, size(popAll, 1), r0,fitness,archive.funvalues);% It produces two random vectors and r1 better than r2
           %%Select a guide particle for each particle
                randindex=zeros(pop_size,1);
                for i = 1 : pop_size
                    RAND=rand(1);
                    TEM=find(RAND<Probablity);
                    randindex(i)=TEM(1);
                    while sorted_index(randindex(i))==i
                        RAND=rand(1);
                        TEM=find(RAND<Probablity);
                        randindex(i)=TEM(1);
                    end
                end
                pbest = pop(sorted_index(randindex), :);

      vi = pop + sf(:, ones(1, problem_size)) .* (pbest - pop + popAll(r1, :) - popAll(r2, :));
      vi = boundConstraint(vi, pop, lu);

      mask = rand(pop_size, problem_size) > cr(:, ones(1, problem_size)); % mask is used to indicate which elements of ui comes from the parent
      rows = (1 : pop_size)'; cols = floor(rand(pop_size, 1) * problem_size)+1; % choose one position where the element of ui doesn't come from the parent
      jrand = sub2ind([pop_size problem_size], rows, cols); mask(jrand) = false;
      ui = vi; ui(mask) = pop(mask);
 
      children_fitness = feval(fhd, ui', func);
       
      children_fitness = children_fitness';

      %%%%%%%%%%%%%%%%%%%%%%%% for out
      for i = 1 : pop_size
	    nfes = nfes + 1;

	if children_fitness(i) < bsf_fit_var
	  bsf_fit_var = children_fitness(i);
	  bsf_solution = ui(i, :);
	end
    
      
    Gbest=[Gbest,bsf_fit_var - optimum];
	if nfes > max_nfes; break; end
      end
      %%%%%%%%%%%%%%%%%%%%%%%% for out

      dif = abs(fitness - children_fitness);


      %% I == 1: the parent is better; I == 2: the offspring is better
      I = (fitness > children_fitness);
      goodCR = cr(I == 1);  
      goodF = sf(I == 1);
      dif_val = dif(I == 1);
 
      archive = updateArchive(archive, popold(I == 1, :), fitness(I == 1));

      [fitness, I] = min([fitness, children_fitness], [], 2);
      
      popold = pop;
      popold(I == 2, :) = ui(I == 2, :);

      num_success_params = numel(goodCR);

      if num_success_params > 0 
	sum_dif = sum(dif_val);
	dif_val = dif_val / sum_dif;

	%% for updating the memory of scaling factor 
    memory_sf=(1-0.1)*memory_sf+0.1*sum(goodF.^2)/sum(goodF);
	%% for updating the memory of crossover rate
    memory_cr=(1-0.1)*memory_cr+0.1*sum(goodCR)/num_success_params;
    
      end


    end

    bsf_error_val = bsf_fit_var - optimum;
    
        len=length(Gbest);
        tem=floor(len/100);
        best=[];
        for i=1:100
            best(i)=Gbest(i*tem);
        end
        best=[best,bsf_error_val];
        fprintf(fileID,'%E\t',best);
        fprintf(fileID,'\r\n');



    fprintf('FVRADE_%d th run, best-so-far error value = %1.8e\n', run_id , bsf_error_val)
    outcome = [outcome bsf_error_val];    
  end %% end 1 run
  fclose(fileID);
  fprintf('\n')
  fprintf('FVRADE_mean error value = %1.8e, std = %1.8e\n', mean(outcome), std(outcome))

end %% end 1 function run
